<?php
/**
 * 用友U8接口 SDK
 * http://u8open.yonyou.com
 * Date: 2019/3/28
 * Time: 16:12
 */



class Yonyouup
{
    private $app_key;
    private $app_secret;
    private $from_account;
    private $to_account;
    var $errmsg;
    const token_key = 'Yonyouup';
    // 	获取Token
    const token_url = 'https://api.yonyouup.com/system/token?';
    // 获取交易号tradeid
    const system_tradeid = 'https://api.yonyouup.com/system/tradeid?';
    // 获取应用订单状态
    // 获取数据源配置
    // 批量获取数据源配置

    // 获取单个U8帐套信息
    const account_get = 'https://api.yonyouup.com/api/account/get?';
    // 批量获取U8帐套信息
    const account_batch_get = 'https://api.yonyouup.com/api/account/batch_get?';
    // 获取单个交易分类信息
    const payunitclass_get = 'https://api.yonyouup.com/api/payunitclass/get?';
    // 批量获取交易分类信息
    const payunitclass_batch_get = 'https://api.yonyouup.com/api/payunitclass/batch_get?';
    // 获取单个交易单位信息
    const payunit_get = 'https://api.yonyouup.com/api/payunit/get?';
    // 批量获取交易单位信息
    const payunit_batch_get = 'https://api.yonyouup.com/api/payunit/batch_get?';
    // 获取单个人员信息
    const person_get = 'https://api.yonyouup.com/api/person/get?';
    // 获取单个人员信息
    const person_batch_get = 'https://api.yonyouup.com/api/person/batch_get?';

    // 获取单个部门
    const department_get = 'https://api.yonyouup.com/api/department/get?';
    // 批量获取部门信息
    const department_batch_get = 'https://api.yonyouup.com/api/department/batch_get?';

    // 本单位开户银行
    const accountingbank_batch_get= 'https://api.yonyouup.com/api/accountingbank/batch_get?';

    // 批量获取供应商
    const vendor_batch_get = 'https://api.yonyouup.com/api/vendor/batch_get?';
    // 获取单个供应商
    const vendor_get = 'https://api.yonyouup.com/api/vendor/get?';

    // 批量获取客户
    const customer_batch_get = 'https://api.yonyouup.com/api/customer/batch_get?';

    // 批量获取应收单
    const oughtreceivelist_batch_get = 'https://api.yonyouup.com/api/oughtreceivelist/batch_get?';
    // 新增一张应收单
    const oughtreceive_add = 'https://api.yonyouup.com/api/oughtreceive/add?';

    // 批量获取财务科目编码
    const code_batch_get = 'https://api.yonyouup.com/api/code/batch_get?';

    // 批量获取自定义项档案
    const define_batch_get = 'https://api.yonyouup.com/api/define/batch_get?';

    // 批量获取结算方式
    const settlestyle_batch_get = 'https://api.yonyouup.com/api/settlestyle/batch_get?';

    // 新增一张应付单
    const oughtpay_add = 'https://api.yonyouup.com/api/oughtpay/add?';
    // 新增一张收款单
    const accept_add = 'https://api.yonyouup.com/api/accept/add?';
    // 新增一张付款单
    const pay_add = 'https://api.yonyouup.com/api/pay/add?';
    // 批量获取银行档案信息
    const bank_batch_get = 'https://api.yonyouup.com/api/bank/batch_get?';
    // 新增一张付款单
    const bank_get = 'https://api.yonyouup.com/api/bank/get?';
    // 批量获取供应商银行信息
    const vendor_bank_batch_get = 'https://api.yonyouup.com/api/vendor_bank/batch_get?';
    // 获取单个供应商银行信息
    const vendor_bank = 'https://api.yonyouup.com/api/vendor_bank/get?';
    // 批量获取客户银行信息
    const customer_bank_batch_get = 'https://api.yonyouup.com/api/customer_bank/batch_get?';
    // 获取单个客户银行信息
    const customer_bank = 'https://api.yonyouup.com/api/customer_bank/get?';

    public function __construct($app_key, $app_secret, $from_account, $to_account)
    {
        $this->app_key = $app_key;
        $this->app_secret = $app_secret;
        $this->from_account = $from_account;
        $this->to_account = $to_account;
        return $this;
    }

    /**
     * 获取Token
     *
     * @return bool|mixed
     */
    public function getToken()
    {
        $para = [
            'from_account' => $this->from_account,
            'app_key' => $this->app_key,
            'app_secret' => $this->app_secret,
        ];
        $token_url = static::token_url . http_build_query($para);
        $resul = static::getRequest($token_url);
        if ($resul['errcode'] == '0') {
            return $resul['token']['id'];
        }
        $this->errmsg = $resul['errmsg'];
        return false;
    }

    /**
     * get请求
     *
     * @param $url
     * @return mixed
     */
    public static function getRequest($url)
    {
        $data = file_get_contents($url);
        return json_decode($data, true);
    }

    /**
     * post 请求
     *
     * @param $url
     * @param $param
     * @return bool|string
     */
    public static function postRequest($url, $param)
    {
        if (empty($url) || empty($param)) {
            return false;
        }
        $postUrl = $url;
        $curlPost = $param;
        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_URL, $postUrl);//抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json; charset=utf-8',
                'Content-Length: ' . strlen($curlPost)
            )
        );
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        $data = curl_exec($ch);//运行curl
        curl_close($ch);
        return $data;
    }

    /**
     * 获取交易号
     *
     * @return bool
     */
    public function getSystemTradeid()
    {
        $para = [
            'from_account' => $this->from_account,
            'app_key' => $this->app_key,
            'token' => $this->getToken(),
        ];
        $tradeid = static::system_tradeid . http_build_query($para);
        $resul = static::getRequest($tradeid);
        if ($resul['errcode'] == '0') {
            return $resul['trade']['id'];
        }
        $this->errmsg = $resul['errmsg'];
        return false;
    }

    /**
     * 获取单个部门信息
     *
     * @param     $depart_id
     * @param int $ds_sequence
     * @return bool|mixed
     */
    public function getDepartment($depart_id, $ds_sequence = 1)
    {
        $para = [
            'from_account' => $this->from_account,
            'to_account' => $this->to_account,
            'app_key' => $this->app_key,
            'token' => $this->getToken(),
            'ds_sequence' => $ds_sequence,
            'id	' => $depart_id,
        ];
        $tradeid = static::department_get . http_build_query($para);
        $resul = static::getRequest($tradeid);
        if ($resul['errcode'] == '0') {
            return $resul['department'];
        }
        $this->errmsg = $resul['errmsg'];
        return false;
    }

    /**
     * 批量取供应商
     *
     * @param array $parame
     * @return bool|mixed
     */
    public function getVendorBatch($parame = [])
    {
        $para = [
            'from_account' => $this->from_account,
            'to_account' => $this->to_account,
            'app_key' => $this->app_key,
            'token' => $this->getToken(),
        ];
        if (!empty($parame)) {
            $para = array_merge($para, $parame);
        }
        $url = static::vendor_batch_get . http_build_query($para);
        $resul = static::getRequest($url);
        if ($resul['errcode'] == '0') {
            return $resul;
        }
        $this->errmsg = $resul['errmsg'];
        return false;
    }

    /**
     * 获取单个供应商
     *
     * @param $id
     * @return bool|mixed
     */
    public function getVendor($id)
    {
        $para = [
            'from_account' => $this->from_account,
            'to_account' => $this->to_account,
            'app_key' => $this->app_key,
            'token' => $this->getToken(),
            'id' => $id,
        ];
        $url = static::vendor_get . http_build_query($para);
        $resul = static::getRequest($url);
        if ($resul['errcode'] == '0') {
            return $resul;
        }
        $this->errmsg = $resul['errmsg'];
        return false;
    }

    /**
     * 批量获取客户信息
     *
     * @param null $parame
     * @return bool|mixed
     */
    public function getCustomerBatch($parame = null)
    {
        $para = [
            'from_account' => $this->from_account,
            'to_account' => $this->to_account,
            'app_key' => $this->app_key,
            'token' => $this->getToken(),
        ];
        if (!empty($parame)) {
            $para = array_merge($para, $parame);
        }
        $url = static::customer_batch_get . http_build_query($para);
        $resul = static::getRequest($url);
        if ($resul['errcode'] == '0') {
            return $resul;
        }
        $this->errmsg = $resul['errmsg'];
        return false;
    }

    /**
     * 拉取应收单
     *
     * @param array $parame
     * @return bool|mixed
     */
    public function getOughtreceivelistBatch($parame = [])
    {
        $para = [
            'from_account' => $this->from_account,
            'to_account' => $this->to_account,
            'app_key' => $this->app_key,
            'token' => $this->getToken(),
        ];
        if (!empty($parame)) {
            $para = array_merge($para, $parame);
        }
        $url = static::oughtreceivelist_batch_get . http_build_query($para);
        $resul = static::getRequest($url);
        if ($resul['errcode'] == '0') {
            return $resul;
        }
        $this->errmsg = $resul['errmsg'];
        return false;
    }

    /**
     * 新增一张应收单
     *
     * @param $data
     * @return bool|string
     */
    public function postOughtreceiveAdd($data)
    {
        $para = [
            'from_account' => $this->from_account,
            'to_account' => $this->to_account,
            'app_key' => $this->app_key,
            'token' => $this->getToken(),
            'tradeid' => $this->getSystemTradeid()
        ];
        $url = static::oughtreceive_add . http_build_query($para);
        $resul = static::postRequest($url, $data);
        return $resul;
    }

    /**
     * 科目编码批量拉取
     *
     * @param array $parame
     * @return bool|mixed
     */
    public function getCodeBatch($parame = [])
    {
        $para = [
            'from_account' => $this->from_account,
            'to_account' => $this->to_account,
            'app_key' => $this->app_key,
            'token' => $this->getToken(),
        ];
        if (!empty($parame)) {
            $para = array_merge($para, $parame);
        }
        $url = static::code_batch_get . http_build_query($para);
        $resul = static::getRequest($url);
        if ($resul['errcode'] == '0') {
            return $resul;
        }
        $this->errmsg = $resul['errmsg'];
        return false;
    }

    /**
     * 批量获取自定义项档案
     *
     * @param array $parame
     * @return bool|mixed
     */
    public function getDefineBatch($parame = [])
    {
        $para = [
            'from_account' => $this->from_account,
            'to_account' => $this->to_account,
            'app_key' => $this->app_key,
            'token' => $this->getToken(),
        ];
        if (!empty($parame)) {
            $para = array_merge($para, $parame);
        }
        $url = static::define_batch_get . http_build_query($para);
        $resul = static::getRequest($url);
        if ($resul['errcode'] == '0') {
            return $resul;
        }
        $this->errmsg = $resul['errmsg'];
        return false;
    }

    /**
     * 批量获取U8帐套信息
     *
     * @param array $parame
     * @return bool|mixed
     */
    public function getAccountBatch($parame = [])
    {
        $para = [
            'from_account' => $this->from_account,
            'to_account' => $this->to_account,
            'app_key' => $this->app_key,
            'token' => $this->getToken(),
        ];
        if (!empty($parame)) {
            $para = array_merge($para, $parame);
        }
        $url = static::account_batch_get . http_build_query($para);
        $resul = static::getRequest($url);
        if ($resul['errcode'] == '0') {
            return $resul;
        }
        $this->errmsg = $resul['errmsg'];
        return false;
    }

    /**
     * 获取单个交易分类信息
     *
     * @param $id
     * @param array $parame
     * @return bool|mixed
     */
    public function getPayunitclass($id, $parame = [])
    {
        $para = [
            'from_account' => $this->from_account,
            'to_account' => $this->to_account,
            'app_key' => $this->app_key,
            'token' => $this->getToken(),
            'id' => $id,
        ];
        $para = array_merge($para, $parame);
        $url = static::payunitclass_get . http_build_query($para);
        $resul = static::getRequest($url);
        if ($resul['errcode'] == '0') {
            return $resul;
        }
        $this->errmsg = $resul['errmsg'];
        return false;
    }

    /**
     * 批量获取交易分类信息
     *
     * @param array $parame
     * @return bool|mixed
     */
    public function getPayunitclassBatch($parame = [])
    {
        $para = [
            'from_account' => $this->from_account,
            'to_account' => $this->to_account,
            'app_key' => $this->app_key,
            'token' => $this->getToken(),
        ];
        if (!empty($parame)) {
            $para = array_merge($para, $parame);
        }
        $url = static::payunitclass_batch_get . http_build_query($para);
        $resul = static::getRequest($url);
        if ($resul['errcode'] == '0') {
            return $resul;
        }
        $this->errmsg = $resul['errmsg'];
        return false;
    }

    /**
     * 获取单个交易单位信息
     *
     * @param $id
     * @param array $parame
     * @return bool|mixed
     */
    public function getPayunit($id, $parame = [])
    {
        $para = [
            'from_account' => $this->from_account,
            'to_account' => $this->to_account,
            'app_key' => $this->app_key,
            'token' => $this->getToken(),
            'id' => $id,
        ];
        $para = array_merge($para, $parame);
        $url = static::payunit_get . http_build_query($para);
        $resul = static::getRequest($url);
        if ($resul['errcode'] == '0') {
            return $resul;
        }
        $this->errmsg = $resul['errmsg'];
        return false;
    }

    /**
     * 批量获取交易单位信息
     *
     * @param array $parame
     * @return bool|mixed
     */
    public function getPayunitBatch($parame = [])
    {
        $para = [
            'from_account' => $this->from_account,
            'to_account' => $this->to_account,
            'app_key' => $this->app_key,
            'token' => $this->getToken(),
        ];
        if (!empty($parame)) {
            $para = array_merge($para, $parame);
        }
        $url = static::payunit_batch_get . http_build_query($para);
        $resul = static::getRequest($url);
        if ($resul['errcode'] == '0') {
            return $resul;
        }
        $this->errmsg = $resul['errmsg'];
        return false;
    }

    /**
     * 获取单个人员信息
     *
     * @param $id
     * @param array $parame
     * @return bool|mixed
     */
    public function getPerson($id, $parame = [])
    {
        $para = [
            'from_account' => $this->from_account,
            'to_account' => $this->to_account,
            'app_key' => $this->app_key,
            'token' => $this->getToken(),
            'id' => $id,
        ];
        $para = array_merge($para, $parame);
        $url = static::person_get . http_build_query($para);
        $resul = static::getRequest($url);
        if ($resul['errcode'] == '0') {
            return $resul;
        }
        $this->errmsg = $resul['errmsg'];
        return false;
    }

    /**
     * 批量获取人员信息
     *
     * @param array $parame
     * @return bool|mixed
     */
    public function getPersonBatch($parame = [])
    {
        $para = [
            'from_account' => $this->from_account,
            'to_account' => $this->to_account,
            'app_key' => $this->app_key,
            'token' => $this->getToken(),
        ];
        if (!empty($parame)) {
            $para = array_merge($para, $parame);
        }
        $url = static::person_batch_get . http_build_query($para);
        $resul = static::getRequest($url);
        if ($resul['errcode'] == '0') {
            return $resul;
        }
        $this->errmsg = $resul['errmsg'];
        return false;
    }

    /**
     * 获取单个人员信息
     *
     * @param $id
     * @param array $parame
     * @return bool|mixed
     */
    public function getAccount($id, $parame = [])
    {
        $para = [
            'from_account' => $this->from_account,
            'to_account' => $this->to_account,
            'app_key' => $this->app_key,
            'token' => $this->getToken(),
            'id' => $id,
        ];
        $para = array_merge($para, $parame);
        $url = static::account_get . http_build_query($para);
        $resul = static::getRequest($url);
        if ($resul['errcode'] == '0') {
            return $resul;
        }
        $this->errmsg = $resul['errmsg'];
        return false;
    }


    /**
     * 新增一张应付单
     *
     * @param $data
     * @return bool|string
     */
    public function postOughtpayAdd($data)
    {
        $para = [
            'from_account' => $this->from_account,
            'to_account' => $this->to_account,
            'app_key' => $this->app_key,
            'token' => $this->getToken(),
            'tradeid' => $this->getSystemTradeid()
        ];
        $url = static::oughtpay_add . http_build_query($para);
        $resul = static::postRequest($url, $data);
        return $resul;
    }

    /**
     * 新增一张收款单
     *
     * @param $data
     * @return bool|string
     */
    public function postAcceptAdd($data)
    {
        $para = [
            'from_account' => $this->from_account,
            'to_account' => $this->to_account,
            'app_key' => $this->app_key,
            'token' => $this->getToken(),
            'tradeid' => $this->getSystemTradeid()
        ];
        $url = static::accept_add . http_build_query($para);
        $resul = static::postRequest($url, $data);
        return $resul;
    }

    /**
     * 新增一张付款单
     *
     * @param $data
     * @return bool|string
     */
    public function postPayAdd($data)
    {
        $para = [
            'from_account' => $this->from_account,
            'to_account' => $this->to_account,
            'app_key' => $this->app_key,
            'token' => $this->getToken(),
            'tradeid' => $this->getSystemTradeid()
        ];
        $url = static::pay_add . http_build_query($para);
        $resul = static::postRequest($url, $data);
        return $resul;
    }

    /**
     * 批量获取结算方式
     *
     * @param array $parame
     * @return bool|mixed
     */
    public function getSettlestyleBatch($parame = [])
    {
        $para = [
            'from_account' => $this->from_account,
            'to_account' => $this->to_account,
            'app_key' => $this->app_key,
            'token' => $this->getToken(),
        ];
        if (!empty($parame)) {
            $para = array_merge($para, $parame);
        }
        $url = static::settlestyle_batch_get . http_build_query($para);
        $resul = static::getRequest($url);
        if ($resul['errcode'] == '0') {
            return $resul;
        }
        $this->errmsg = $resul['errmsg'];
        return false;
    }

    /**
     * 批量获取银行档案信息
     *
     * @param array $parame
     * @return bool|mixed
     */
    public function getBankBatch($parame = [])
    {
        $para = [
            'from_account' => $this->from_account,
            'to_account' => $this->to_account,
            'app_key' => $this->app_key,
            'token' => $this->getToken(),
        ];
        if (!empty($parame)) {
            $para = array_merge($para, $parame);
        }
        $url = static::bank_batch_get . http_build_query($para);
        $resul = static::getRequest($url);
        if ($resul['errcode'] == '0') {
            return $resul;
        }
        $this->errmsg = $resul['errmsg'];
        return false;
    }

    /**
     * 获取单个银行档案信息
     *
     * @param $id
     * @param array $parame
     * @return bool|mixed
     */
    public function getBank($id, $parame = [])
    {
        $para = [
            'from_account' => $this->from_account,
            'to_account' => $this->to_account,
            'app_key' => $this->app_key,
            'token' => $this->getToken(),
            'id' => $id,
        ];
        $para = array_merge($para, $parame);
        $url = static::bank_get . http_build_query($para);
        $resul = static::getRequest($url);
        if ($resul['errcode'] == '0') {
            return $resul;
        }
        $this->errmsg = $resul['errmsg'];
        return false;
    }


    /**
     * 批量获取供应商银行信息
     *
     * @param array $parame
     * @return bool|mixed
     */
    public function getVendorBankBatch($parame = [])
    {
        $para = [
            'from_account' => $this->from_account,
            'to_account' => $this->to_account,
            'app_key' => $this->app_key,
            'token' => $this->getToken(),
        ];
        if (!empty($parame)) {
            $para = array_merge($para, $parame);
        }
        $url = static::vendor_bank_batch_get . http_build_query($para);
        $resul = static::getRequest($url);
        if ($resul['errcode'] == '0') {
            return $resul;
        }
        $this->errmsg = $resul['errmsg'];
        return false;
    }

    /**
     * 获取单个供应商银行信息
     *
     * @param $id
     * @param array $parame
     * @return bool|mixed
     */
    public function getVendorBank($id, $parame = [])
    {
        $para = [
            'from_account' => $this->from_account,
            'to_account' => $this->to_account,
            'app_key' => $this->app_key,
            'token' => $this->getToken(),
            'id' => $id,
        ];
        $para = array_merge($para, $parame);
        $url = static::vendor_bank . http_build_query($para);
        $resul = static::getRequest($url);
        if ($resul['errcode'] == '0') {
            return $resul;
        }
        $this->errmsg = $resul['errmsg'];
        return false;
    }

    /**
     * 批量获取客户银行信息
     *
     * @param array $parame
     * @return bool|mixed
     */
    public function getCustomerBankBatch($parame = [])
    {
        $para = [
            'from_account' => $this->from_account,
            'to_account' => $this->to_account,
            'app_key' => $this->app_key,
            'token' => $this->getToken(),
        ];
        if (!empty($parame)) {
            $para = array_merge($para, $parame);
        }
        $url = static::customer_bank_batch_get . http_build_query($para);
        $resul = static::getRequest($url);
        if ($resul['errcode'] == '0') {
            return $resul;
        }
        $this->errmsg = $resul['errmsg'];
        return false;
    }

    /**
     * 获取单个客户银行信息
     *
     * @param $id
     * @param array $parame
     * @return bool|mixed
     */
    public function getCustomerBank($id, $parame = [])
    {
        $para = [
            'from_account' => $this->from_account,
            'to_account' => $this->to_account,
            'app_key' => $this->app_key,
            'token' => $this->getToken(),
            'id' => $id,
        ];
        $para = array_merge($para, $parame);
        $url = static::customer_bank . http_build_query($para);
        $resul = static::getRequest($url);
        if ($resul['errcode'] == '0') {
            return $resul;
        }
        $this->errmsg = $resul['errmsg'];
        return false;
    }

    /**
     * 批量获取客户银行信息
     *
     * @param array $parame
     * @return bool|mixed
     */
    public function getAccountingbankBatch($parame = [])
    {
        $para = [
            'from_account' => $this->from_account,
            'to_account' => $this->to_account,
            'app_key' => $this->app_key,
            'token' => $this->getToken(),
        ];
        if (!empty($parame)) {
            $para = array_merge($para, $parame);
        }
        $url = static::accountingbank_batch_get . http_build_query($para);
        $resul = static::getRequest($url);
        if ($resul['errcode'] == '0') {
            return $resul;
        }
        $this->errmsg = $resul['errmsg'];
        return false;
    }

}